// basic file operations
#include <stdio.h>
#include "xpmio.h"

// maps 0-63 to chars for use in XPM creation
static char charmap[64] = {'0','1','2','3','4','5','6','7',
						   '8','9','A','B','C','D','E','F',
						   'G','H','I','J','K','L','M','N',
						   'O','P','Q','R','S','T','U','V',
						   'W','X','Y','Z','a','b','c','d',
						   'e','f','g','h','i','j','k','l',
						   'm','n','o','p','q','r','s','t',
						   'u','v','w','x','y','z','+','-'};

// write an XPM with dimensions height and length to file filename using data
void writeXPM(double **data, int height, int length, char* filename) {
	int i, j;
	FILE *file;
	file = fopen(filename,"w");

	fprintf(file, "%s", "/* XPM */\n");
	fprintf(file, "%s%s%s", "static char * ", filename, "_xpm[] = {\n");
	fprintf(file, "%s%d%s%d%s", "\"", length, " ", height, " 64 1\",\n");
	fprintf(file, "%s", "\"0\tc #000000\",\n");
	fprintf(file, "%s", "\"1\tc #040404\",\n");
	fprintf(file, "%s", "\"2\tc #080808\",\n");
	fprintf(file, "%s", "\"3\tc #0C0C0C\",\n");
	fprintf(file, "%s", "\"4\tc #101010\",\n");
	fprintf(file, "%s", "\"5\tc #141414\",\n");
	fprintf(file, "%s", "\"6\tc #181818\",\n");
	fprintf(file, "%s", "\"7\tc #1C1C1C\",\n");
	fprintf(file, "%s", "\"8\tc #202020\",\n");
	fprintf(file, "%s", "\"9\tc #242424\",\n");
	fprintf(file, "%s", "\"A\tc #282828\",\n");
	fprintf(file, "%s", "\"B\tc #2C2C2C\",\n");
	fprintf(file, "%s", "\"C\tc #303030\",\n");
	fprintf(file, "%s", "\"D\tc #343434\",\n");
	fprintf(file, "%s", "\"E\tc #383838\",\n");
	fprintf(file, "%s", "\"F\tc #3C3C3C\",\n");
	fprintf(file, "%s", "\"G\tc #404040\",\n");
	fprintf(file, "%s", "\"H\tc #444444\",\n");
	fprintf(file, "%s", "\"I\tc #484848\",\n");
	fprintf(file, "%s", "\"J\tc #4C4C4C\",\n");
	fprintf(file, "%s", "\"K\tc #505050\",\n");
	fprintf(file, "%s", "\"L\tc #545454\",\n");
	fprintf(file, "%s", "\"M\tc #585858\",\n");
	fprintf(file, "%s", "\"N\tc #5C5C5C\",\n");
	fprintf(file, "%s", "\"O\tc #606060\",\n");
	fprintf(file, "%s", "\"P\tc #646464\",\n");
	fprintf(file, "%s", "\"Q\tc #686868\",\n");
	fprintf(file, "%s", "\"R\tc #6C6C6C\",\n");
	fprintf(file, "%s", "\"S\tc #707070\",\n");
	fprintf(file, "%s", "\"T\tc #747474\",\n");
	fprintf(file, "%s", "\"U\tc #787878\",\n");
	fprintf(file, "%s", "\"V\tc #7C7C7C\",\n");
	fprintf(file, "%s", "\"W\tc #808080\",\n");
	fprintf(file, "%s", "\"X\tc #848484\",\n");
	fprintf(file, "%s", "\"Y\tc #888888\",\n");
	fprintf(file, "%s", "\"Z\tc #8C8C8C\",\n");
	fprintf(file, "%s", "\"a\tc #909090\",\n");
	fprintf(file, "%s", "\"b\tc #949494\",\n");
	fprintf(file, "%s", "\"c\tc #989898\",\n");
	fprintf(file, "%s", "\"d\tc #9C9C9C\",\n");
	fprintf(file, "%s", "\"e\tc #A0A0A0\",\n");
	fprintf(file, "%s", "\"f\tc #A4A4A4\",\n");
	fprintf(file, "%s", "\"g\tc #A8A8A8\",\n");
	fprintf(file, "%s", "\"h\tc #ACACAC\",\n");
	fprintf(file, "%s", "\"i\tc #B0B0B0\",\n");
	fprintf(file, "%s", "\"j\tc #B4B4B4\",\n");
	fprintf(file, "%s", "\"k\tc #B8B8B8\",\n");
	fprintf(file, "%s", "\"l\tc #BCBCBC\",\n");
	fprintf(file, "%s", "\"m\tc #C0C0C0\",\n");
	fprintf(file, "%s", "\"n\tc #C4C4C4\",\n");
	fprintf(file, "%s", "\"o\tc #C8C8C8\",\n");
	fprintf(file, "%s", "\"p\tc #CCCCCC\",\n");
	fprintf(file, "%s", "\"q\tc #D0D0D0\",\n");
	fprintf(file, "%s", "\"r\tc #D4D4D4\",\n");
	fprintf(file, "%s", "\"s\tc #D8D8D8\",\n");
	fprintf(file, "%s", "\"t\tc #DCDCDC\",\n");
	fprintf(file, "%s", "\"u\tc #E0E0E0\",\n");
	fprintf(file, "%s", "\"v\tc #E4E4E4\",\n");
	fprintf(file, "%s", "\"w\tc #E8E8E8\",\n");
	fprintf(file, "%s", "\"x\tc #ECECEC\",\n");
	fprintf(file, "%s", "\"y\tc #F0F0F0\",\n");
	fprintf(file, "%s", "\"z\tc #F4F4F4\",\n");
	fprintf(file, "%s", "\"+\tc #F8F8F8\",\n");
	fprintf(file, "%s", "\"-\tc #FCFCFC\"");

	for(i = 0; i < height; i++) {
		fprintf(file, "%s", ",\n\"");
		for(j = 0; j < length; j++) {
			if((int)data[i][j] < 0) {
				fprintf(file, "%c", charmap[0]);
			}
			else if((int)data[i][j] > 63) {
				fprintf(file, "%c", charmap[63]);
			}
			else {
				fprintf(file, "%c", charmap[(int)data[i][j]]);
			}
		}
		fprintf(file, "%s", "\"");
	}
	fprintf(file, "%s", "\"};");
	fclose(file);
}
